const GameDataManger = require("./GameDataManger");


cc.Class({
    extends: cc.Component,

    properties: {
        newBg:{     //参赛空位底
            default:null,
            type:cc.Sprite
        },

        headIcon:{     //头像
            default:null,
            type:cc.Sprite
        },

        timeBg:{     //时间底
            default:null,
            type:cc.Node
        },

        timeLab:{     //时间倒计时
            default:null,
            type:cc.Label
        },

        // nameLab:{     //玩家名称
        //     default:null,
        //     type:cc.Label
        // },

        red:{     //左侧红色
            default:null,
            type:cc.Node
        },

        blue:{     //右侧蓝色
            default:null,
            type:cc.Node
        },

        leftState:{     //左侧状态
            default:null,
            type:cc.Sprite
        },

        rightState:{     //右侧状态
            default:null,
            type:cc.Sprite
        },

        stageSps:{     //状态图片
            default:[],
            type:[cc.SpriteFrame]
        },

        addIcon:{     //提示进入icon
            default:null,
            type:cc.Node
        },

        chatLeft:{     //提示进入icon
            default:null,
            type:cc.Node
        },

        chatRight:{     //提示进入icon
            default:null,
            type:cc.Node
        },

    },

    start () {
        this.times = 0;

        this.headUrl = ""

        this.data = {};

        this.type = 0

        this.isLeft = true

        this.node.on(cc.Node.EventType.TOUCH_START,this.clickHandler, this);
    },
    
    // type //0参赛席位  //1结果席位， isLeft 左边，右边， state 0胜， 1负， 2平  time 倒计时
    refresh(type, isLeft, data, state){
        var self = this;
        this.red.active = false;
        this.blue.active = false;
        this.type = type;     
        this.isLeft = isLeft; 

        // if (isLeft){
        //     this.red.active = true;
        // }else{
        //     this.blue.active = true;
        // }

        //self.headIcon.spriteFrame = null;

        this.data = data;
        this.leftState.node.active = false;
        this.rightState.node.active = false;
        // 远程 url 带图片后缀名
        if (data){
            var remoteUrl = data.headUrl;
        
            //头像
            // if (this.headUrl != remoteUrl && remoteUrl != ""){
            //     cc.assetManager.loadRemote(remoteUrl, function (err, texture) {
            //         if (err) {  
            //             console.log('加载远程图片失败:', err);  
            //             return;  
            //         }  
            //         // 加载成功后，创建精灵帧  
            //         var spriteFrame = new cc.SpriteFrame(texture);  
            //         self.headIcon.spriteFrame = spriteFrame;
    
            //         this.headUrl = remoteUrl;
            //     });
            // }
        }

        //胜负平
        if(type == 1){
            if (isLeft){
                this.leftState.node.active = true;
                this.leftState.spriteFrame = this.stageSps[state];
            }else{
                this.rightState.node.active = true;
                if (state == 2){
                    this.rightState.spriteFrame = this.stageSps[2];
                }else{
                    this.rightState.spriteFrame = this.stageSps[state == 0 ? 1 : 0];
                }
            }
        }
    },

    //展示表情
    showEmoj(data){
        if (data && this.data && data.userId == this.data.userId){
            this.chatLeft.active = false;
            this.chatRight.active = false;
            if (this.isLeft){
                this.showEmojChat(this.chatLeft, data.id);
            }else{
                this.showEmojChat(this.chatRight, data.id);
            }
        }
    },

    showEmojChat(chatNode, id){
        chatNode.active = true;
        chatNode.getChildByName("emojSp").active = false;
        chatNode.getChildByName("emojLab").active = false;
        if (id < 100){
            chatNode.getChildByName("bg").width = 150;
            chatNode.getChildByName("emojSp").active = true;

            let url = 'imags/emoji/emo' + id
            GameDataManger.loadIamge(chatNode.getChildByName("emojSp").getComponent(cc.Sprite), url);
        }else{
            chatNode.getChildByName("bg").width = 450;
            chatNode.getChildByName("emojLab").active = true;

            let cfg = GameDataManger.getChatCfgById(id);
            chatNode.getChildByName("emojLab").getComponent(cc.Label).string = cfg.des;
        }

        cc.tween(chatNode)
        .delay(1.5)
        .call(()=>{
            chatNode.active = false;
        })
        .start();
    },

    setAddIcon(isShow){
        if(isPlaying){
            this.addIcon.active = false;
        }else{
            this.addIcon.active = isShow;
        }
    },

    stopTime(){
        this.timeBg.active = false;
        this.remainingTime = 20;

        remainTime = this.remainingTime;
        this.unschedule(this.updateCountdown)

    },

    refreshTime(time){
        //todo 屏蔽倒计时
        return;
        if(time > 0){
            this.remainingTime = time;

            remainTime = this.remainingTime;
            this.timeBg.active = true;
            this.timeLab.string = this.remainingTime + "s";
            this.updateCountdown();
            this.schedule(this.updateCountdown, 1);
        }else{
            this.timeBg.active = false;
            this.unschedule(this.updateCountdown)
        }
    },

    updateCountdown() {  
        if (!this.timeBg.active){
            return;
        }
        if (this.remainingTime > 0) {  
            this.remainingTime -= 1;  
            remainTime = this.remainingTime;
            this.timeLab.string = this.remainingTime + "s";
            //this.scheduleOnce(this.updateCountdown, 1);  // 继续每秒调用一次  
        } else {   
            // 在这里添加倒计时结束后的逻辑  
            this.timeBg.active = false;
            if (selfUserId == roomUserId){
                cc.systemEvent.emit('ai_chess');
            }
            this.unschedule(this.updateCountdown, this)
        }  
    }, 

    clickHandler(){
        if (isPlaying){
            return;
        }
        if (this.type == 0){
            if (this.isLeft && players[0] == null){
                cc.systemEvent.emit('sendMessage', 1007, [0]);
            }if (!this.isLeft && players[1] == null){
                cc.systemEvent.emit('sendMessage', 1007, [1]);
            }else{

            }
        }
    }
});
